﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

public class CraftTableInfo : MonoBehaviour
{
    public static CraftTableInfo _instance;

    public List<CraftTable> CraftTable_dic = new List<CraftTable>();

    public TextAsset objectstext;
    public static string Textasset_path = "Assets/Resources/crafttable.txt";

    void Awake()
    {
        _instance = this;
        ReadInfo();
        //Debug.Log(CraftTable_dic.Count);
    }

    public CraftTable getCraftinfobyid(int id){
        CraftTable info = null;

        int front = 0;
        int after = CraftTable_dic.Count - 1;
        int mid;
        while (front <= after)
        {
            mid = (front + after) / 2;
            if (CraftTable_dic[mid].id == id)
            {
                info = CraftTable_dic[mid];
                break;
            }
            else if (CraftTable_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return info;
    }

    public int Check_info_is_Existed(int id)
    {
        int front = 0;
        int after = CraftTable_dic.Count - 1;
        int mid;
        while (front <= after)
        {
            mid = (front + after) / 2;

            if (CraftTable_dic[mid].id == id)
            {
                return mid;
            }
            else if (CraftTable_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return -1;
    }
    // 把合成表的内容排序
    public void Sort_Craft_List()
    {
        CraftTable_dic.Sort(
            (left, right) =>
            {
                if (left.id > right.id) return 1;
                else if (left.id == right.id) return 0;
                else return -1;
            });
    }


    public int Create_new_craft()
    {
        int test_index = 1;
        while (Check_info_is_Existed(test_index) >= 0) test_index++;

        CraftTable table = new CraftTable(test_index);
        CraftTable_dic.Add(table);

        return table.id;
    }
    public void ReadInfo()
    {
        CraftTable_dic.Clear();

        string text = objectstext.text;
        string[] strArray = text.Split('\n');

        foreach(string str in strArray)
        {
            string[] proArray = str.Split(',');
            CraftTable craft = new CraftTable(int.Parse(proArray[0]));

            craft.id = int.Parse(proArray[0]);
            craft.targetid = int.Parse(proArray[1]);
            int number = int.Parse(proArray[2]);
            craft.num_type = number;
            for(int i = 0; i < number; i++)//将合成材料信息读入
            {
                craft.mat_id.Add(int.Parse(proArray[2 * i + 3]));
                craft.mat_num.Add(int.Parse(proArray[2 * i + 4]));
            }
            CraftTable_dic.Add(craft);//添加到字典中
        }
    }
    // 将更改后的道具信息存储回
    public void WriteInfo()
    {
        string result = "";
        int index = 0;

        foreach(CraftTable craft_info in CraftTable_dic)
        {
            string one_line = "";
            one_line += craft_info.id.ToString();
            one_line += ",";
            one_line += craft_info.targetid.ToString();
            one_line += ",";
            one_line += craft_info.num_type.ToString();
            one_line += ",";
            for(int i = 0; i < craft_info.num_type; i++)
            {
                one_line += craft_info.mat_id[i];
                one_line += ",";
                one_line += craft_info.mat_num[i];
                one_line += ",";
            }
            result += one_line;
            result += "\n";
            index++;
        }
        result = result.Remove(result.Length - 1);
        System.IO.File.WriteAllText(Textasset_path, result);
    }

}
//id 名称  图标名称 类型（药品）	加血量 加辐射 加饱食 加饱水 止血 辐射抑制    抑制时间 止疲劳



public class CraftTable
{
    public int id;//合成表id
    public int targetid;//合成id
    public int num_type;//合成材料的种类
    public List<int> mat_id = new List<int>();
    public List<int> mat_num = new List<int>();

    public CraftTable(int test_index)
    {
        id = test_index;
        targetid = 0;
        num_type = 0;
        mat_id = new List<int>();
        mat_num = new List<int>();
    }
}